package mvc_generator.generator.car.struts

import mvc_generator.generator.MVCGeneratorGeneratorUtils
import mvc_generator.mVCGenerator.Form
import mvc_generator.mVCGenerator.ModelMVC
import mvc_generator.mVCGenerator.View
import org.eclipse.xtext.generator.IFileSystemAccess
import mvc_generator.mVCGenerator.AssignedField

class ViewGenerator {

	//nom des actions de chaque formulaire : /actionForm«view.name»«i = i+1»
	StrutsGenerator g; //utile pour lui transmettre les lignes a ecrire 

	//dans le fichier Resources.properties 
	new(StrutsGenerator gen) {
		g = gen;
	}

	def void generate(View view, IFileSystemAccess fsa) {
		var ModelMVC modelMVCContainer = MVCGeneratorGeneratorUtils.findModelMVCContainer(view);
		var CharSequence jspContent = ''''''; //génération de la page jsp
		jspContent = '''
%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="nested" uri="http://struts.apache.org/tags-nested"%>
<html:html>
<head>
<title><bean:message key="«view.name».title" /></title>
<html:base />
</head>
<body bgcolor="white">
«IF view.forms.size == 0»
«ELSE»
«FOR Form form : view.forms»
<html:form action="/actionForm«view.name»«form.name»">
<html:errors />
</html:form>
<table>
«FOR AssignedField assignedField : form.assignedFields»
<tr>
<td align="right">«assignedField.fieldName»</td>
<td align="left"><html:text property="«assignedField.fieldName»" size="30"
maxlength="30" /></td>
</tr>
«ENDFOR» 
<tr>
<td align="right"><html:submit>Save</html:submit></td>
</tr>
</table>
«ENDFOR»
«ENDIF»
</body>
</html:html>
		'''

		//génération du fichier jsp
		fsa.generateFile(modelMVCContainer.name+"Struts"+StrutsGenerator.pagesPath + view.name + ".jsp", jspContent);

		//ajout des lignes pour la génération du fichier de ressources
		g.addLineToResourcesContentFile(view.name + ".title=" + view.name);
	}
}
